﻿'Clinciu Andrei

Option Strict On

Imports Gift.BL
Imports System.Configuration
Imports System.IO
Imports System.Xml

Public Class frmProducts

    Private Sub FillItems()
         flvProducts.Items.Clear()
        flvProducts.Colums = {"Naam", "Omschrijving", "Prijs", "Beschikbaar"}
        Dim list As List(Of Product) = Product.GetProductsFromDatabase(frmMain.dbh)
        For Each theProduct As Product In list
            ' CStr(IIf(Not theProduct.Onbeschikbaar, "Ja", "Nee"))
            Dim flvi As New FilteredListViewItem({theProduct.Naam, theProduct.Omschrijving, CStr(theProduct.Prijs), CStr(IIf(theProduct.Onbeschikbaar, "Ja", "Nee"))}, theProduct)
            'flvProducts.lstView.Items.Add(flvi)
            flvProducts.Items.Add(flvi)
        Next
        flvProducts.FillListItems()
    End Sub

 
    Private Sub EditItem()
        Dim dialog As New frmProductExport
        dialog.Product = CType(flvProducts.SelectedObject, Product)
        'You can't remove or add other Occasions
        dialog.chkGelegenheid.Enabled = False
        dialog.isNew = False
        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Product.ChangeProduct(dialog.Product, dialog.GetOccasions, frmMain.dbh)
        Else
            ' User clicked the Cancel button
        End If
        FillItems()
    End Sub

    Public Sub AddItem()
        Dim dialog As New frmProductExport
        dialog.Product = New Product(0, "", "", "", CDec(0.0), True)

        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Product.AddProductToDb(dialog.Product, dialog.GetOccasions, frmMain.dbh)
        Else
            ' User clicked the Cancel button
        End If
        FillItems()
    End Sub


    Private Sub DeleteItem()
        'if no item is selected.. then don't bother
        If flvProducts.lstView.SelectedItems.Count <> 0 Then
            'look if you can delete it
            If Product.CanDelete(CType(flvProducts.SelectedObject, Product), frmMain.dbh) = False Then
                'delete it
                Product.DeleteProduct(CType(flvProducts.SelectedObject, Product), frmMain.dbh)
                MessageBox.Show("Het product werd verwijderd", "Product verwijderd", MessageBoxButtons.OK, MessageBoxIcon.Information)
                FillItems()
            Else
                MessageBox.Show("Je mag deze product niet verwijderen.", "FOUT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Selecteer een item om te verwijderen.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If
    End Sub
    Private Sub ExportToExcel()
        sfd.InitialDirectory = Application.StartupPath
        sfd.Title = "Selecteer het excelfile die u wilt opslaan"

        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Product.ExportToExcel(frmMain.dbh, CType(cboGelegenheid.SelectedItem, Occasion), sfd.FileName)
            MessageBox.Show("Alle producten voor het gelegenheid" & CType(cboGelegenheid.SelectedItem, Occasion).Name & "werden geëxporteerd.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
    'Import from excel to the database..
    Private Sub ImportFromExcel()
        ofd.InitialDirectory = Application.StartupPath
        ofd.Title = "Selecteer het excelfile die u wilt opslaan"
        ofd.Filter = "Excel (*.xlsx)|*.xlsx|Excel (*.xls)|*.xls"

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Product.ImportFromExcel(frmMain.dbh, ofd.FileName)
            MessageBox.Show("Alle producten zijn geïmporteerd.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        FillItems()
    End Sub
#Region "Events"

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddItem()
    End Sub
    Private Sub frmProducts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillItems()
        FillOccasions()
    End Sub
    Private Sub FillOccasions()
        For Each item As Occasion In Occasion.SelectAllOccasions(frmMain.dbh)
            cboGelegenheid.Items.Add(item)
        Next
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If flvProducts.lstView.SelectedItems.Count <> 0 Then
            EditItem()
        Else
            MessageBox.Show("Selecteer een item om aan te passen", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteItem()
    End Sub
    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        If cboGelegenheid.SelectedIndex = -1 Then
            MessageBox.Show("Selecteer een gelegenheid om te kunnen exporteren", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            ExportToExcel()
        End If
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        ImportFromExcel()
    End Sub
#End Region


End Class